// pages/goodsManage/goodsTypeChoose/goodsTypeChoose.js
const request = require("../../../utils/network.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastX: 0,          //滑动开始x轴位置
    lastY: 0,          //滑动开始y轴位置
    typeList: null,
    open: true,
    acitiveId: "",
    sndList: null,
    isGoods: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var parms = {
      fatherId: ''
    };
    var url = '/goodsType/getGoodsTypeList';
    request.request(url, parms, function (res) {
      that.setData({
        typeList: res.data
      })
      // 动画
    }, function () {
      wx.showToast({
        title: '加载数据失败',
      })
    }, 'GET')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarColor({
      "frontColor": "#ffffff",
      "backgroundColor": "#f5b97f",
      "animation": {}
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

 // 二级菜单
  gotoNext: function(event){
    var that = this;
    var id = event.currentTarget.dataset.id;
    var index = event.currentTarget.dataset.index;
    var parms = {
      fatherId: id
    };
    var url = '/goodsType/getGoodsTypeList';
    request.request(url, parms, function (res) {
      that.setData({
        sndList: res.data,
        open: true
      });
    }, function () {
      wx.showToast({
        title: '加载数据失败',
      })
    }, 'GET')
  },
  // 三级菜单
  
  gotoNextThi: function(event){
    var that = this;
    var isGoods = that.data.isGoods
    var url,parms;
    var id = event.currentTarget.dataset.id;
    var index = event.currentTarget.dataset.index;
    if(!isGoods){
      parms = {
        fatherId: id
      };
      url = '/goodsType/getGoodsTypeList';
      request.request(url, parms, function (res) {
        if (res.data.length != 0) {
          that.setData({
            sndList: res.data,
            open: true
          });
        } else {
          that.setData({
            isGoods: true
          });
          parms = {
            typeId: id
          };
          url = '/goods/getGoodsList';
          request.request(url, parms, function (res) {
            that.setData({
              sndList: res.data
            })
          }, function () {
            wx.showToast({
              title: '加载数据失败',
            })
          }, 'POST')
        }
      }, function () {
        wx.showToast({
          title: '加载数据失败',
        })
      }, 'GET')
    }else{
      wx.navigateTo({
        url: '../goodsStyleList/goodsStyleList?goodsId=' + id
      })
    }
  },
  //左右滑动事件
  tap_start:function(event){
    this.setData({
      lastX: event.changedTouches[0].clientX,
      lastY: event.changedTouches[0].clientY
    })
  },
  tap_end: function(event){
    var currentX = event.changedTouches[0].clientX;
    var currentY = event.changedTouches[0].clientY;
    if (currentX - this.data.lastX <- 50 && Math.abs(currentY - this.data.lastY) < 50){
        //向左滑动
        this.setData({
          open: false
        })
      }
    else if (currentX - this.data.lastX > 50 && Math.abs(currentY - this.data.lastY)<50){
        // 向右滑动
        this.setData({
          open: true
        })
    }
  },
  setFirstNavActive: function(index){
    var that = this;
    for(let i =0;i<that.data.typeList.length;i++){
      
    }
    if(index){
      that.data.typeList[index].isActive = true; 
    }
  }
})